package com.foreverwin.jzymes.archive;

import com.foreverwin.jzymes.archive.domain.DataArchive;
import com.foreverwin.jzymes.archive.service.IDataArchiveAdviceService;
import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.framework.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArchiveDataService {

    @Autowired
    IDataArchiveAdviceService dataArchiveAdviceService;
    @Autowired
    IProductionShiftService productionShiftService;

    public void archiveData( String site ){

        Map<String,Object> paramPool = new HashMap<>();
        Date now = new Date();
        Date[] dateRange = productionShiftService.findShiftRange( site, now );
        paramPool.put( "CURRENT_SHIFT_START_TIME", dateRange[0] );
        paramPool.put( "CURRENT_SHIFT_END_TIME", dateRange[1] );
        paramPool.put( "CURRENT_DATE", DateUtil.parse( DateUtil.format( now, "yyyy-MM-dd" ) ) );
        paramPool.put( "CURRENT_TIME", new Date() );
        paramPool.put( "SITE", site );

        //获取当前表所有关联表
        //List<Future> futureList = new ArrayList<>();
        List<DataArchive> dataArchiveList = dataArchiveAdviceService.findReferDataArchiveList( null );
        for( DataArchive dataArchive : dataArchiveList ){
            ArchiveTask task = new ArchiveTask( dataArchive, paramPool );
            task.run();
            /*FutureTask future = new FutureTask( task, null );
            Future future = AsyncManager.me().submit( task );
            futureList.add( future );
            new Thread( future ).start();*/
        }

        /*for( Future future : futureList ){
            try {
                future.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }*/
    }
}
